﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Medical.EntityType;
using Sector7.Framework.Enums;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Standard;
using Sector7.Framework.Utility;

namespace Sector7.Medical.ADDM.Windows
{
    /// <summary>
    /// AddInterviewWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddInterviewTemplateWindow : Window
    {
        public AddInterviewTemplateWindow()
        {
            InitializeComponent();
        }

        private void LoadInterviewTemplate(T7_InterviewTemplate interviewTemplate)
        {
            txbTemplateName.Text = interviewTemplate.TemplateName;
            txbPercentForInterview.Text = interviewTemplate.PercentOfInterview.ToString();
            txbPercentForWritten.Text = interviewTemplate.PercentOfWritten.ToString();
        }

        public T7_InterviewTemplate InterviewTemplate
        {
            set
            {
                m_template = value;
                LoadInterviewTemplate(m_template);
            }
            get { return m_template; }
        }

        public CrudType Crud
        {
            get { return m_crud; }
            set { m_crud = value; }
        }

        private T7_InterviewTemplate m_template = null;
        private CrudType m_crud = CrudType.Unknown;

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            var newTemplate = new T7_InterviewTemplate();
            newTemplate.TemplateName = txbTemplateName.Text;
            newTemplate.PercentOfInterview = Convert.ToDecimal(txbPercentForInterview.Text);
            newTemplate.PercentOfWritten = Convert.ToDecimal(txbPercentForWritten.Text);

            object result = null;

            switch (m_crud)
            {
                case CrudType.Create:
                    result = DAOFactory.GetDAO<InterviewTemplateDAO>().Create(newTemplate);
                    newTemplate.TemplateId = Convert.ToInt32(result);
                    break;
                case CrudType.Update:
                    newTemplate.TemplateId = m_template.TemplateId;

                    var delta = ClientUtil.GetDelta(m_template, newTemplate);
                    if (delta.DeltaProperties.Count > 0)
                    {
                        delta.Conditions.Add("TemplateId", m_template.TemplateId);
                        result = DAOFactory.GetDAO<InterviewTemplateDAO>().Update(delta);
                    }
                    break;
                default:
                    return;
            }

            m_template = newTemplate;

            this.DialogResult = result != null;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
